<?php 
/**
 * Cette classe gère les personnalités qui ont agit dans le monde de Disney
 * 
 * @author Filament
 * @version 0.1
 * 
 * 10/06/11 : création de la page
 */

class Personnalite {
	private $id;
	private $nom;
	private $prenom;
	private $naissance;
	private $mort;
	private $description;
	private $photo;
	private $moyenne;
	private $id_post_biographie;
	private $votes = array();
	
	public function __construct() {

		//En fonction du nombre d'arguements on ne va pas dans la même fonction :
		$num=func_num_args();
		
		switch($num) {
			case 1 :
				$tmp_id = func_get_arg(0);
				$this->newPersonnalite($tmp_id);
				break;
			case 8 :
				$nom = func_get_arg(0);
				$prenom = func_get_arg(1);
				$naissance = func_get_arg(2);
				$mort = func_get_arg(3);
				$description = func_get_arg(4);
				$photo = func_get_arg(5);
				$moyenne = func_get_arg(6);
				$id_post_biographie = func_get_arg(7);
				$this->ajoutePersonnalite($nom, $prenom, $naissance, $mort, $description, $photo, $moyenne, $id_post_biographie);
		}
	}
	
	private function newPersonnalite($id) {
		global $db;
		
		//on prend ce dont on a besoin :
		$sql = "SELECT 
					id_personnalite
					, nom
					, prenom
					, naissance
					, mort
					, description
					, description_uid
					, description_bitfield
					, photo
					, moyenne
					, id_post_biographie
				FROM
					".PERSONNALITE_TABLE."
				WHERE
					id_personnalite = '".$id."'
				LIMIT 0,1";
		$result = $db->sql_query($sql);
		$datas = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);
		
		//On affecte les attributs :
		$this->id = $id;
		$this->nom = $data['nom'];
		$this->prenom = $data['prenom'];
		$this->naissance = $data['naissance'];
		$this->mort = $data['mort'];
		$this->photo = $data['photo'];
		$this->moyenne = $data['moyenne'];
		$this->id_post_biographie = $data['id_post_biographie'];
		//Et hop bbcodisons ce texte :
		$bbcode = new bbcode(base64_encode($data['description_bitfield']));
		$bbcode->bbcode_second_pass($data['description'], $data['description_uid'], $data['description_bitfield']);
		$texte = bbcode_nl2br($data['description']);
		$texte = smiley_text($texte);
		$this->description = $texte;
		
		//Maintenant nous prenons les informations pour faire le tableau des votes : 
		$sql = "SELECT
					note
					, COUNT(*) as nb_votes
				FROM
					".VOTE_TABLE." v
					, ".PERSONNALITE_VOTE_TABLE." p
				WHERE
						p.id_personnalite = '".$this->id."'
					AND
						p.id_vote = v.id_vote
				GROUP BY
					note
				ORDER BY
					note";
		$result = $db->sql_query($sql);
		while($data = $db->sql_fetchrow($result))
			$this->votes[$data['note']] = $data['nb_votes'];
		$db->sql_freeresult($result);
	}
	
	public function getId() {
		return $this->id;
	}
	
	public function getNom() {
		return $this->nom;
	}
	
	public function getPrenom() {
		return $this->prenom;
	}
	
	public function getNaissance() {
		return $this->naissance;
	}
	
	public function getMort() {
		return $this->mort;
	}
	
	public function getPhoto() {
		return $this->photo;
	}
	
	public function getMoyenne() {
		return $this->moyenne;
	}
	
	public function getBiographie() {
		global $db;
		
		$sql = "SELECT
					p.post_text
					, p.bbcode_uid
					, p.bbcode_bitfield
					, p.poster_id
					, u.username
					, p.post_time
					, p.post_edit_time
				FROM
					".USERS_TABLE." u
					, ".POSTS_TABLE." p
				WHERE
						p.post_id = '".$this->id_post_biographie."'
					AND
						p.poster_id = u.user_id
				LIMIT 0,1";
		$result = $db->sql_query($sql);
		$data = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);
		
		$biographie['username'] = $data['username'];
		$biographie['poster_id'] = $data['poster_id'];
		//Et hop bbcodisons ce texte :
		$bbcode = new bbcode(base64_encode($data['bbcode_bitfield']));
		$bbcode->bbcode_second_pass($data['post_text'], $data['bbcode_uid'], $data['bbcode_bitfield']);
		$texte = bbcode_nl2br($data['post_text']);
		$texte = smiley_text($texte);
		
		$biographie['message'] = $texte;
		
		return $biographie;
	}
	
	public function getDescription() {
		return $this->description;
	}
	
	public function getMetiers() {
		global $db;
		
		$sql = "SELECT
					DISTINCT m.id_metier
					, m.nom
				FROM
					".METIERS_TABLE." m
					, ".PERSONNALITE_FILM_TABLE." pf
					, ".PERSONNALITE_ATTRACTION_TABLE." pa
				WHERE
					(	pf.id_personnalite = '".$this->id."'
					AND
						pf.id_metier = m.id_metier)
					OR
					(	pa.id_personnalite = '".$this->id."'
					AND
						pa.id_metier = m.id_metier)";
		$result = $db->sql_query($sql);
		while($data = $db->sql_fetchrow($result))
			$metiers[] = array($data['id_metier'], $data['nom']);
		$db->sql_freeresult($result);
		
		return $metiers;
	}
	
	public function getFilms() {
		global $db;
		
		//Les informations sur le film :
		$sql = "SELECT
					DISTINCT pf.id_film
				FROM
					".PERSONNALITE_FILM_TABLE." pf
				WHERE
					pf.id_personnalite = '".$this->id."'
				";
		$result = $db->sql_query($sql);
		while($data = $db->sql_fetchrow($result)) {
			$metier = array();
			//Quels métiers ai je fait pour ce film ?
			$metiersSQL = "SELECT
								m.id_metier
								, m.nom
							FROM
								".METIERS_TABLE." m
								, ".PERSONNALITE_FILM_TABLE." pf
							WHERE
									pf.id_metier = '".$this->id."'
								AND
									pf.id_film = '".$data['id_film']."'";
			$resultMetiers = $db->sql_query($metiersSQL);
			while($dataMetier = $db->sql_fetchrow($resultMetiers))
				$metiers[] = array($dataMetiers['id_metier'], $dataMetier['nom']);
			$films[] = array(new Film($data[id_film]), $metiers);
		}
		
		return $films;
	}
	
	public function getAttractions() {
		global $db;
		
		//Les informations sur le film :
		$sql = "SELECT
					DISTINCT pa.id_attraction
				FROM
					".PERSONNALITE_ATTRACTION_TABLE." pa
				WHERE
					pa.id_personnalite = '".$this->id."'
				";
		$result = $db->sql_query($sql);
		while($data = $db->sql_fetchrow($result)) {
			$metier = array();
			//Quels métiers ai je fait pour ce film ?
			$metiersSQL = "SELECT
								m.id_metier
								, m.nom
							FROM
								".METIERS_TABLE." m
								, ".PERSONNALITE_ATTRACTION_TABLE." pa
							WHERE
									pa.id_metier = '".$this->id."'
								AND
									pa.id_film = '".$data['id_film']."'";
			$resultMetiers = $db->sql_query($metiersSQL);
			while($dataMetier = $db->sql_fetchrow($resultMetiers))
				$metiers[] = array($dataMetiers['id_metier'], $dataMetier['nom']);
			$films[] = array(new Film($data[id_film]), $metiers);
		}
		
		return $films;
	}
	
	public function getProjets() {
		global $db;
		
		$projets = $this->getFilms();
		$projets .= $this->getAttractionss();
		
		return $films;
	}
}
?>